package com.chencs.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chencs.vo.biz.product.KeyModel;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.jsoup.Jsoup;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by fangzehua on 2017/8/24.
 */
public class HttpPostGiftCode {


    public static String http(String code) {
        String str = "-1";
        try {
            org.jsoup.nodes.Document res = Jsoup.connect("http://taokouling.com/index.php?m=api&a=GetTkljm")
                    .data("username", "6185129")
                    .data("password", "78745511")
                    .data("text", code)
                    .timeout(10000).ignoreContentType(true).post();
            str = res.body().text();
            System.out.println(str);
        } catch (Exception e) {
            e.printStackTrace();
            return "-1";
        }
        return str;
    }

    /**
     * 小白鼠
     * @param code
     * @return
     */
    public static String http2(String code) {
        String str = "-1";
        try {
         //   System.out.println("http://www.2v1.top/tkl.php?tkl="+code+"&type=1");
            org.jsoup.nodes.Document res = Jsoup.connect("http://www.2v1.top/tkl.php?tkl="+code+"&type=1")
                    .timeout(10000).ignoreContentType(true).get();
            str = res.body().text();

        } catch (Exception e) {
            e.printStackTrace();
            return "-1";
        }
        return str;
    }



    /**
     * 轻淘客拿券
     * @param sid
     * @param gid
     * @return
     */
    public static String QingTaoKe(String sid,String gid) {
        String str1 = "-1";
        try {
            org.jsoup.nodes.Document res = Jsoup.connect("http://www.qingtaoke.com/api/UserPlan/UserCouponList")
                    .data("gid", gid)
                    .data("sid", sid)
                    .timeout(40000).ignoreContentType(true).post();

            str1 = res.body().text();
        } catch (Exception e) {
            e.printStackTrace();
            return "-1";
        }
        return str1;
    }




    /**
     * 券描述
     * @param aid
     * @param gid
     * @return
     */
    public static String getPwdInfo(String aid,String gid) {
        String str1 = "-1";
        try {
            org.jsoup.nodes.Document res = Jsoup.connect("https://uland.taobao.com/cp/coupon?activityId="+aid+"&itemId="+gid)
             .timeout(40000).ignoreContentType(true).get();
            str1 = res.body().text();

        } catch (Exception e) {
            e.printStackTrace();
            return "-1";
        }
        return str1;
    }


    public static String XPDR(String code) throws  Exception {
        String requestStr = "{\"content\":\"44CQ5oiR5YmB5omL6YO96KaB5Lmw55qE5a6d6LSd77yI5paw5qy+5LyR6Zey6IO45YyF55S36Z+p54mI6IWw5YyF55qu6LSo5bCP5YyF5YyF55S35aOr5pac5oyO5YyF5Y2V6IKp5YyF6L+Q5Yqo6IOM5YyF5r2u5YyF77yJ77yM5b+r5p2l5ZKM5oiR5LiA6LW355Oc5YiG57qi5YyF44CR77yM5aSN5Yi26L+Z5p2h5L+h5oGv77+lYVlyUjBldmRNSWLvv6XlkI7miZPlvIDmiYvmnLrmt5jlrp0=\"}";
        //构造HttpClient的实例
        HttpClient httpClient = new HttpClient();

        //创建GET方法的实例
        PostMethod getMethod = new PostMethod("https://api.huiwancun.net/HwcWxappService/taobaoapp/parseProduct.api");
        RequestEntity entity = new StringRequestEntity(requestStr,"application/json","UTF-8");
        getMethod.setRequestEntity(entity);
        getMethod.setRequestHeader("Content-Type","application/json;charset=UTF-8");
        new HttpClient().executeMethod(getMethod);
        String str =getMethod.getResponseBodyAsString();
        return  str;

//        try {
//            org.jsoup.nodes.Document res = Jsoup.connect("https://api.huiwancun.net/HwcWxappService/taobaoapp/parseProduct.api")
//                    .header("Accept", "*/*")
//                    .header("Accept-Encoding", "gzip, deflate")
//                    .header("Accept-Language","zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3")
//                    .header("Accept-Language","zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3")
//                    .header("Content-Type", "application/json")
//                    .data("content", "{ \"content\":\"44CQ5oiR5YmB5omL6YO96KaB5Lmw55qE5a6d6LSd77yI5paw5qy+5LyR6Zey6IO45YyF55S36Z+p54mI6IWw5YyF55qu6LSo5bCP5YyF5YyF55S35aOr5pac5oyO5YyF5Y2V6IKp5YyF6L+Q5Yqo6IOM5YyF5r2u5YyF77yJ77yM5b+r5p2l5ZKM5oiR5LiA6LW355Oc5YiG57qi5YyF44CR77yM5aSN5Yi26L+Z5p2h5L+h5oGv77+lYVlyUjBldmRNSWLvv6XlkI7miZPlvIDmiYvmnLrmt5jlrp0=\"}")
//                    .timeout(10000).ignoreContentType(true).post();
//            str1 = res.body().text();
//        } catch (Exception e) {
//            e.printStackTrace();
//            return "-1";
//        }
//




    }


//        String url = "http://www.taokouling.com/index.php?m=api&a=taokoulingjm";
//        Map<String, String> params = new HashMap<String, String>();
//        params.put("username", "6185129");
//        params.put("password", "78745511");
//        params.put("text", code);
//        URL u = null;
//        HttpURLConnection con = null;
//        // 构建请求参数
//        StringBuffer sb = new StringBuffer();
//        if (params != null) {
//            for (Map.Entry<String, String> e : params.entrySet()) {
//                sb.append(e.getKey());
//                sb.append("=");
//                sb.append(e.getValue());
//                sb.append("&");
//            }
//            sb.substring(0, sb.length() - 1);
//        }
//        // 尝试发送请求
//        try {
//            u = new URL(url);
//            con = (HttpURLConnection) u.openConnection();
//            //// POST 只能为大写，严格限制，post会不识别
//            con.setRequestMethod("POST");
//            con.setDoOutput(true);
//            con.setDoInput(true);
//            con.setUseCaches(false);
//            con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
//            OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream(), "UTF-8");
//            osw.write(sb.toString());
//            osw.flush();
//            osw.close();
//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally {
//            if (con != null) {
//                con.disconnect();
//            }
//        }
//        // 读取返回内容
//        StringBuffer buffer = new StringBuffer();
//        try {
//            //一定要有返回值，否则无法把请求发送给server端。
//            BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
//            String temp;
//            while ((temp = br.readLine()) != null) {
//                buffer.append(temp);
//                buffer.append("\n");
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

      //  return str;



//    public static void main(String[] args) {
//
//        HttpPostGiftCode.http("【我剁手都要买的宝贝（运动内衣女美背无钢圈收副乳防下垂调整型文胸聚拢小胸性感上托），快来和我一起瓜分红I包】http://m.tb.cn/h.WEOgMbJ    点击链接，再选择浏览器打开；或复制这条信息￥CWLh0qwmvPn￥后打开手淘");
//    }

}
